R workshop - 1

R as a calculator

In [ ]:
10+5
10-5
4*6
16/4
2^8
In [ ]:
2*5-(2^5)/8

Logical operators

In [ ]:
2 == 2 # is it equal?
3 > 2 # is it larger?
3 < 1 # is it smaller?
4 != 0 # is it not equal?

Variables and funcions

Lets define some variables

In [ ]:
x <- 1
y <- 2
str1 <- "hello world"
In [ ]:
print( str1 )
x; y; str1
In [ ]:
z <- x+y
z
In [ ]:
z = z + 1
In [ ]:
z

Variables stored in our workspace

In [1]:
ls() 
#or
objects()
In [ ]:
rm(z) #remove
# save.image() # save variables in the workspace
#q() #quits the session
# load( ".RData" ) # load the workspace
In [ ]:
z
In [ ]:
ls()

Built-in functions

In [ ]:
sqrt(1024) # square root
log(4) # Ln
mean(c(100,50,35)) # mean
exp(2) #e^2
round( exp(2), digits=1 ) # most built-in functions have options

Getting some help with built-in functions

In [ ]:
?round # documentation
In [ ]:
help(round)

We can also built functions

In [ ]:
mySEM <- function(x){
return(sd(x)/sqrt(length(x)))
}
In [ ]:
example_list <- c(1,5,8,2,4,5,6,7,44,1,5,6,7,8,6,8,9)
mySEM(example_list)

Where are we working?

In [ ]:
getwd() # get working directory
dir <- getwd()
In [ ]:
setwd("/home/jovyan/work/data/") # set working directory
getwd() 
list.files(path = ".")
In [ ]:
setwd("/home/jovyan/work") # set working directory
getwd()
In [ ]:
current_dir="."
list.files(path = current_dir)

Executing script from file

In [ ]:
myCofV(example_list)
In [ ]:
source("my-functions.R") # run from script
In [ ]:
myCofV(example_list)

There are various types of objects

In [ ]:
class( 2 )  # find the object type of v1
class( c(1,5,7) )
mode("Hello")  # and similarly
mode(2==2)
mode(TRUE)

Vectors

In [ ]:
x <- c(1, 2, 3) # Combine Values into a Vector or List
In [ ]:
mean(x)
min(x)
max(x)
In [ ]:
s <- c( min(x),max(x) )
In [ ]:
s

Operationson vector elements

In [ ]:
v1 <- c(1,2,3)
c1 <- c( "a", "b", "c", "d" )
In [ ]:
v1 * 3 
toupper( c1 )

Matrixes

In [ ]:
testVector<-c(1,2,3,4,5,6,7,8,9)
m1 <- matrix( testVector, nrow=3, ncol=3 ) # matrix: table of n rows and m columns, only one type of data allowed and equal length  required
colnames(m1)<-c('1','2','3')
rownames(m1)<-c('A','B','C')
m1
In [ ]:
cbind(m1,"4"=c(1,2,3))
rbind(m1,"D"=c(6,5,4))

Data frame

In [ ]:
var1 <- c( "John", "Paul", "George", "Ringo" )
var2 <- c( 123456, 234567, 345678, 456789 )
var3 <- c( 8.5, 9.0, 5.0, 6.75 )
classroom <- data.frame( ID=var2, Score=var3 ) #data.frame: table of n rows and c columns, different types per column allowed
rownames( classroom ) <- var1
In [ ]:
classroom

Retrieving data

In [ ]:
classroom[1]
In [ ]:
classroom$ID # each column can be accessed with its name
In [ ]:
classroom[,1]
In [ ]:
classroom[1,]
In [ ]:
classroom[1,2]
In [ ]:
classroom[classroom$ID==234567,]
In [ ]:
classroom["Paul",]
In [ ]:
classroom["Paul","Score"]

Activity

In [ ]:
RECORDS <- data.frame(
ID=seq(from=1, to=40, by=1),
Sex=c(rep("Male",times=20),rep("Female",times=20)), 
Age=round(rnorm(40, mean=55, sd=5),digits=0),
Score=c(rnorm(20, mean=70, sd=10),rnorm(20, mean=20, sd=15))
)
In [ ]:
RECORDS
In [ ]:
# 1 Get a new table just for women
# 2 Select only the indivuduals with score higher than the mean score 
# 3 Select only the men with a score higher than 70 and age lower than 55
# 4 Order all individuals by score